package com.upcode.weighing.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.upcode.weighing.domain.SysRole;
import com.upcode.weighing.mapper.SysRoleMapper;
import com.upcode.weighing.service.SysRoleService;
import com.upcode.weighing.vo.add.SysRoleAddVo;
import com.upcode.weighing.vo.del.BaseDelVo;
import com.upcode.weighing.vo.edit.SysRoleEditVo;
import com.upcode.weighing.vo.req.PageReqVo;
import com.upcode.weighing.vo.req.pc.SysRoleListVo;
import com.upcode.weighing.vo.resp.PageRespVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

import static com.upcode.weighing.common.constant.BillConstants.DELETE_TYPE_NOT_DELETE;

/**
 * <p>
 * 角色表 服务实现类
 * </p>
 *
 * @author alex
 * @since 2019-04-10
 */
@Service
@Transactional
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements SysRoleService {

    @Autowired
    private SysRoleMapper sysRoleMapper;

    //添加角色
    @Transactional(readOnly = false,rollbackFor = Exception.class)
    @Override
    public int addRole(SysRoleAddVo addVo) {
        SysRole role=new SysRole();
        role.setId(IdWorker.getId());//角色ID
        role.setName(addVo.getName());//角色名称
        role.setCode(addVo.getCode());//角色编码
        role.setSortNum(1);//角色排序 默认设置为1
        role.setDes(addVo.getDes());//备注
        role.setStatus(DELETE_TYPE_NOT_DELETE);//状态  默认设置为1  可用
        role.setCreateTime(LocalDateTime.now());//创建时间 默认当前时间
        role.setUpdateTime(LocalDateTime.now());//修改时间 默认当前时间
        return sysRoleMapper.addRole(role);
    }

    //修改角色
    @Transactional(readOnly = false,rollbackFor = Exception.class)
    @Override
    public int updateRole(SysRoleEditVo vo) {
        SysRole sysRole=new SysRole();
        sysRole.setId(vo.getId());//角色ID
        sysRole.setName(vo.getName());//角色名称
        sysRole.setSortNum(vo.getSortNum());//排序
        sysRole.setDes(vo.getDes());//描述
        sysRole.setStatus(vo.getStatus());//状态
        sysRole.setUpdateTime(LocalDateTime.now());//更新时间
        return sysRoleMapper.updateRole(sysRole);
    }

    //删除角色
    @Override
    public int delRole(BaseDelVo vo) {
        return sysRoleMapper.delRole(vo.getIds());
    }

    //角色列表
    @Override
    public List<SysRole> roleList() {
        return sysRoleMapper.roleList();
    }
    //根据ID查看角色详情
    @Override
    public SysRole getRoleById(Long id) {
        return sysRoleMapper.getRoleById(id);
    }

    //角色分页列表
    @Override
    public PageRespVo<SysRole> list(PageReqVo<SysRoleListVo> vo) {
        Page<SysRoleListVo> pageInfo = new Page<>(vo.getPage().getCurrentPage(), vo.getPage().getPageSize());//创建分页
        PageRespVo<SysRole> pageRespVo = new PageRespVo<>();
        IPage<SysRole> iPage = sysRoleMapper.selectPageList(pageInfo,vo.getParams());
        pageRespVo.setPage(iPage);
        return pageRespVo;
    }
    //根据用户查询对应的角色
    @Override
    public List<SysRole> getRoleByUser(Long userId) {
        return sysRoleMapper.getRoleByUser(userId);
    }
    //判断角色是否存在(根据角色名和角色编码)
    @Override
    public List<SysRole> checkRoleCode(String name,String code) {
        return sysRoleMapper.checkRoleCode(name,code);
    }
}
